package prictise.com.application1;

import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Environment;

import com.google.protobuf.InvalidProtocolBufferException;

import java.io.File;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.net.URLEncoder;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;

import prictise.com.application1.bean.Student;
import prictise.com.protobuf.DataInfo;

/**
 * @author zhisiyi
 * @date 18.6.24 13:15
 * @comment
 */
public class TestCase {

//    public static String getBASE64(String str) {
//        if (str == null) return null;
//        return (new sun.misc.BASE64Encoder()).encode( str.getBytes() );
//    }

  @Test
  public void testArrayCopy() {
//  System.arraycopy(elements, 0, a, r, p);
//构造对象
    DataInfo.Student student = DataInfo.Student.newBuilder()
            .setName("LeoLee")
            .setAge(25)
            .setAddress("上海")
            .build();

    byte[] student2ByteArray = student.toByteArray();
    DataInfo.Student student2 = null;
    try {
      student2 = DataInfo.Student.parseFrom(student2ByteArray);
    } catch (InvalidProtocolBufferException e) {
      e.printStackTrace();
    }

    System.out.println(student2);
    System.out.println(student2.getName());
    System.out.println(student2.getAge());
    System.out.println(student2.getAddress());
  }

  @Test
  public void testReferenceQueue() {
    long a = 6922281190050557661L;
    System.out.println((int) a);
//    weakReferenceTest();

    // /storage/emulated/0/Tencent/WeixinWork/imagecache/imagemsg2/f/f07f2a471c71ce1c0b9645db19744784.0.temp
    String s = "/storage/emulated/0/Tencent/WeixinWork/imagecache/imagemsg2/f/f07f2a471c71ce1c0b9645db19744784.0.temp";
    int tempIndex = s.lastIndexOf(".temp");
    System.out.println(tempIndex);
    System.out.println(s.substring(0, tempIndex));
  }

  private void weakReferenceTest() {
    ReferenceQueue<Object> rq = new ReferenceQueue<>();
    Object testObject = new Object();
    WeakReference<Object> wro = new WeakReference<>(testObject, rq);
    System.out.println(wro);

//    testObject = null;

    Runtime.getRuntime().gc();

    try {
      Thread.sleep(100);
    } catch (Throwable t) {
      System.out.println(t);
    }

    while (rq.poll() != null) {
      Object o = rq.poll();
      System.out.println("rq o = " + o);
    }
  }
}
